package com.software.project.controller;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.google.gson.Gson;
import com.software.project.domain.Result;
import com.software.project.entity.Suit;
import com.software.project.entity.UserInfo;
import com.software.project.entity.Weapon;
import com.software.project.service.UserInfoService;
import com.software.project.service.WeaponService;
import com.software.project.utils.LogUtil;
import com.software.project.utils.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/weapon")
@Slf4j
public class WeaponController {
    @Autowired
    private WeaponService weaponService;
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private HttpServletRequest request;
    private String[] numbers={"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p"};
    //根据武器id查找武器的方法
    @GetMapping("/findWeaponByWeaponId")
    public Weapon findWeaponByWeaponId(Integer weaponId) {
        Weapon weapon = weaponService.findWeaponByWeaponId(weaponId);
        if (weapon != null) {
            log.info(LogUtil.success(request.getRemoteAddr(), weaponId + ""));
        } else {
            // 非法查询
            log.info(LogUtil.error(request.getRemoteAddr(), weaponId + ""));
        }
        return weapon;
    }
    //根据武器图片路径查找武器的方法
    @GetMapping("/findWeaponByWeaponImage")
    public Weapon findWeaponByWeaponImage(String wearWeapon){
        Weapon weapon=weaponService.findWeaponByWeaponImage(wearWeapon);
        if (weapon != null) {
            log.info(LogUtil.success(request.getRemoteAddr(), wearWeapon));
        } else {
            // 非法查询
            log.info(LogUtil.error(request.getRemoteAddr(), wearWeapon));
        }
        return weapon;
    }
    @GetMapping("/findAllWeapon")
    public Result findAllWeapon(String userId){
        //通过WeaponController的三个方法获取已拥有的武器list 可解锁的武器list 不可解锁的武器list
        List<Weapon> unlockedWeaponList=findUnlockedWeapon(userId);
        List<Weapon> unlockingWeaponList=findUnlockingWeapon(userId);
        List<Weapon> lockedWeaponList=findLockedWeapon(userId);
        //创建Gson对象 用于将List转换为json数据 传回前端
        Gson weaponGson=new Gson();
        //将三个list转换为json数组
        JSONArray unlockedWeaponArray=new JSONArray(weaponGson.toJson(unlockedWeaponList));
        JSONArray unlockingWeaponArray=new JSONArray(weaponGson.toJson(unlockingWeaponList));
        JSONArray lockedWeaponArray=new JSONArray(weaponGson.toJson(lockedWeaponList));
        //创建一个存放三种状态的武器数组的JSONObject
        JSONObject allWeapon=new JSONObject();
        //将三种状态的武器数组存进allWeapon
        allWeapon.put("unlocked",unlockedWeaponArray);
        allWeapon.put("unlocking",unlockingWeaponArray);
        allWeapon.put("locked",lockedWeaponArray);
        log.info(LogUtil.success(request.getRemoteAddr(), userId));
        return ResultUtil.success("200","返回武器数据成功",allWeapon);
    }
    //查找用户已拥有的武器
    @GetMapping("/findUnlockedWeapon")
    public List<Weapon> findUnlockedWeapon(String userId){
        //根据用户id拿到用户对象
        UserInfo userInfo=userInfoService.findUserInfoByUserId(userId);
        //拿到个人武器库
        String weaponStore=userInfo.getWeaponStore();
        JSONObject jsonObject=new JSONObject(weaponStore);
        List<Weapon> list=new ArrayList<Weapon>();
        //把个人武器库的json的每个key对应的值找出来 这就是武器id  用武器id找到武器 存入list
        for (String key:jsonObject.keySet()) {
            Integer integer=jsonObject.getInt(key);
            Weapon weapon=weaponService.findWeaponByWeaponId(integer);
            list.add(weapon);
        }
        log.info(LogUtil.success(request.getRemoteAddr(), userId));
        return list;
    }
    //查找可解锁的武器
    @GetMapping("/findUnlockingWeapon")
    public List<Weapon> findUnlockingWeapon(String userId){
        //根据用户id拿到用户对象
        UserInfo userInfo=userInfoService.findUserInfoByUserId(userId);
        //拿到个人武器库
        String weaponStore=userInfo.getWeaponStore();
        //传入类型进行强转 这里拿到的是已拥有和可解锁混合的武器list
        List<Weapon> scopeWeapon=weaponService.findUnlockingWeapon((int)userInfo.getIntelligence(),(int)userInfo.getPower());
        JSONObject jsonObject=new JSONObject(weaponStore);
        //把个人武器库的json的每个key对应的值找出来 这就是武器id  从混合武器list里删除掉这些武器id对应的武器对象
        for (String key:jsonObject.keySet()) {
            Integer integer=jsonObject.getInt(key);
            Weapon weapon=weaponService.findWeaponByWeaponId(integer);
            scopeWeapon.remove(weapon);
        }
        log.info(LogUtil.success(request.getRemoteAddr(), userId));
        return scopeWeapon;
    }
    //查找不可解锁的武器
    @GetMapping("/findLockedWeapon")
    public List<Weapon> findLockedWeapon(String userId){
        //根据用户id拿到用户对象
        UserInfo userInfo=userInfoService.findUserInfoByUserId(userId);
        //传入类型进行强转
        List<Weapon> scopeWeapon=weaponService.findLockedWeapon((int)userInfo.getIntelligence(),(int)userInfo.getPower());
        log.info(LogUtil.success(request.getRemoteAddr(), userId));
        return scopeWeapon;
    }

    //点击解锁按钮 默认这里是系统帮我们筛选出来了 所以不用判断了
    @GetMapping("/unlockWeapon")
    public Result unlockWeapon(String userId, Integer weaponId){
        //根据用户id获取用户对象
        UserInfo userInfo=userInfoService.findUserInfoByUserId(userId);
        //获取用户武器库
        String weaponStore=userInfo.getWeaponStore();
        //将武器库转成json对象
        JSONObject jsonObject=new JSONObject(weaponStore);
        //把转过来的武器id存入json对象
        jsonObject.put(numbers[weaponId-1],weaponId);
        //设置新的武器库
        userInfo.setWeaponStore(jsonObject.toString());
        //更新武器库
        UserInfo userInfoTemp=userInfoService.createUserInfo(userInfo);
        log.info(LogUtil.success(request.getRemoteAddr(), userId + " " + weaponId));
        return ResultUtil.success("200","解锁成功",null);
    }

    //点击穿戴按钮
    @GetMapping("/wearWeapon")
    public Result wearWeapon(String userId, Integer weaponId){
        //根据用户id获取用户对象
        UserInfo userInfo=userInfoService.findUserInfoByUserId(userId);
        //根据武器id获取新武器对象
        Weapon weapon=findWeaponByWeaponId(weaponId);
        if(userInfo.getWearWeapon().equals(weapon.getWeaponSrc())){
            log.info(LogUtil.error(request.getRemoteAddr(), userId + " " + weaponId));
            return ResultUtil.success("404","您已经穿戴上这件武器了",null);
        }
        //根据用户穿戴武器的图片路径获取旧武器对象
        Weapon oldWeapon=findWeaponByWeaponImage(userInfo.getWearWeapon());
        //重新设置用户魅力值
        userInfo.setCharm(userInfo.getCharm()-oldWeapon.getWeaponCharm()+weapon.getWeaponCharm());
        userInfo.setComprehensive(userInfo.getIntelligence()*0.5+userInfo.getPower()*0.3+userInfo.getCharm()*0.2);
        //给当前对象更改穿戴武器的图片
        userInfo.setWearWeapon(weapon.getWeaponSrc());
        //更新用户穿戴武器的图片和魅力值和综合素质
        UserInfo userInfoTemp=userInfoService.createUserInfo(userInfo);
        log.info(LogUtil.success(request.getRemoteAddr(), userId + " " + weaponId));
        return ResultUtil.success("200","穿戴成功",null);
    }
}
